<template>
  <div class="EchartPractice">
    <div id="main"></div>
  </div>
</template>

<script>
// 在项目终端安装echarts：npm install echarts --save

// 在 main.js 中全局引入 echarts在这里插入图片描述
// import echarts from "echarts";
// Vue.prototype.$echarts = echarts;

// 错误："export ‘default’ (imported as ‘echarts’) was not found in ‘echarts’，解决办法：
// 卸载： npm uninstall echarts
// 重装echarts： npm install echarts@4.9.0

export default {
  name: "EchartPractice",
  methods: {
    drawChart() {
      let myEchart = this.$echarts.init(document.getElementById("main"));
      let option = {
        title: {
          text: "Vue中使用Echarts",
        },
        tooltip: {},
        legend: {
          data: ["销量"],
        },
        xAxis: {
          data: ["苹果", "香蕉", "橘子", "葡萄"],
        },
        yAxis: {},
        series: [
          {
            name: "销量",
            type: "bar",
            data: [50, 60, 45, 30],
          },
        ],
      };
      myEchart.setOption(option);
    },
  },
  mounted() {
    this.drawChart();
  },
};
</script>

<style scoped>
#main {
  width: 600px;
  height: 400px;
  margin: auto;
  margin-top: 100px;
}
</style>